package com.juzhi.dvrapp.utils;

import android.content.Context;

import com.juzhi.dvrapp.bean.DeviceModel;
import com.juzhi.dvrapp.bean.MessageModel;
import com.juzhi.dvrapp.bean.dao.DaoMaster;
import com.juzhi.dvrapp.bean.dao.DaoSession;
import com.juzhi.dvrapp.bean.dao.MessageModelDao;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

/**
 * Created by YangHu on 2017/9/8.
 */

public class GreenDaoUtil {

    private static final String DEFAULT_DB_NAME = "DVRCamera-dao.db";
    private static String DB_NAME;
    private static Context mContext;

    private static DaoMaster daoMaster;
    private static DaoSession daoSession;

    public static void init(Context context) {
        init(context, DEFAULT_DB_NAME);
    }

    public static void init(Context context, String dbName) {
        if (context == null) {
            throw new IllegalArgumentException("context can't be null");
        }
        mContext = context.getApplicationContext();
        DB_NAME = dbName;
    }

    private static DaoMaster getDaoMaster() {
        if (daoMaster == null) {
            //此处不可用 DaoMaster.DevOpenHelper, 那是开发辅助类，我们要自定义一个，方便升级
            DaoMaster.OpenHelper helper = new MyOpenHelper(mContext, DB_NAME);
            daoMaster = new DaoMaster(helper.getEncryptedReadableDb("SigmaWit"));
        }
        return daoMaster;
    }

    private static DaoSession getDaoSession() {
        if (daoSession == null) {
            if (daoMaster == null) {
                daoMaster = getDaoMaster();
            }
            daoSession = daoMaster.newSession();
        }
        return daoSession;
    }

    public static void enableQueryBuilderLog() {
        QueryBuilder.LOG_SQL = true;
        QueryBuilder.LOG_VALUES = true;
    }

    /**
     * 添加设备
     *
     * @param model
     */
    public static void insertDevice(DeviceModel model) {
        getDaoSession().getDeviceModelDao().insert(model);
    }

    /**
     * 删除设备
     *
     * @param model
     */
    public static void deleteDevice(DeviceModel model) {
        getDaoSession().getDeviceModelDao().delete(model);
    }

    /**
     * 更新指定设备
     */
    public static void updateDevice(DeviceModel model) {
        getDaoSession().getDeviceModelDao().update(model);
    }

    /**
     * 查询所有的设备
     */
    public static List<DeviceModel> queryDeviceList() {
        QueryBuilder<DeviceModel> deviceModelQueryBuilder = getDaoSession().getDeviceModelDao().queryBuilder();
        return deviceModelQueryBuilder.list();
    }

    /**
     * 添加消息
     */
    public static void insertMessage(MessageModel model) {
        getDaoSession().getMessageModelDao().insert(model);
    }

    /**
     * 删除消息
     *
     * @param model
     */
    public static void deleteMessage(MessageModel model) {
        getDaoSession().getMessageModelDao().delete(model);
    }


    /**
     * 查询所有的消息
     */
    public static List<MessageModel> queryMessageList() {
        QueryBuilder<MessageModel> MessageModelQueryBuilder = getDaoSession().getMessageModelDao().queryBuilder();
        return MessageModelQueryBuilder.list();
    }

    /**
     * 查询某个设备的消息
     */
    public static List<MessageModel> queryMessage2DeviceList(String ssid) {
        QueryBuilder<MessageModel> MessageModelQueryBuilder = getDaoSession().getMessageModelDao().queryBuilder().where(MessageModelDao.Properties.Ssid.eq(ssid));
        return MessageModelQueryBuilder.list();
    }

    /**
     * 查询设备某天的消息
     *
     * @param time
     * @return
     */
    public static List<MessageModel> queryMessage2DayList(String ssid, long time) {
        QueryBuilder<MessageModel> MessageModelQueryBuilder = getDaoSession().getMessageModelDao().queryBuilder();
        MessageModelQueryBuilder.where(MessageModelDao.Properties.Ssid.eq(ssid),
                MessageModelQueryBuilder.and(MessageModelDao.Properties.Time.ge(time), MessageModelDao.Properties.Time.le(time + 86400)));
        return MessageModelQueryBuilder.list();
    }

    /**
     * 查询指定SSID 进行time降序排序   分页加载  一页加载10条
     */
    public static List<MessageModel> getTwentyRec(String ssid, int offset){
        return getDaoSession().getMessageModelDao().queryBuilder().where(MessageModelDao.Properties.Ssid.eq(ssid)).orderDesc(MessageModelDao.Properties.Time).offset(offset * 10).limit(10).list();
    }

}
